package com.hellobike.haxing.user.impl;


import com.hellobike.haxing.common.ResponseCodeEnum;
import com.hellobike.haxing.order.result.Result;
import com.hellobike.haxing.user.ILoginService;
import com.hellobike.haxing.user.api.LoginApi;
import com.hellobike.haxing.user.request.VerificationCodeRequest;
import com.hellobike.haxing.user.response.UserResponse;
import com.hellobike.haxing.user.response.VerficationCodeResponse;
import com.hellobike.haxing.user.vo.UserRegisterVo;
import com.hellobike.haxing.user.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import static com.hellobike.haxing.common.ResponseCodeEnum.VERFICATION_ERROR;

@Service
@Slf4j
public class LoginServiceImpl implements ILoginService {

    @Reference(validation = "true")
    private LoginApi loginApi;

    @Override
    public Result<String> verficationCode(VerificationCodeRequest request) {
        Result<String> result = new Result<>();
        VerficationCodeResponse response = loginApi.sendVerficationCode(request);
        if (!response.isSuccess() && !StringUtils.isBlank(response.getCode())) {
            result.faild(response.getCode(), VERFICATION_ERROR.code(), VERFICATION_ERROR.message());
        } else if (!response.isSuccess()) {
            result.faild(null, ResponseCodeEnum.SYSTEM_ERROR.code(), "短信限制，请明天再试");
        } else {
            result.success("");
        }
        return result;
    }

    @Override
    public Result<UserVo> login(UserRegisterVo request) {
        Result result = new Result();
        request.setLoginType(2);
        request.setPlatform(2);
        try{
            UserResponse response = loginApi.login(request);
            if (response.isSuccess()) {
                result.success("").setData(response.getUserVo());
            } else {
                result.faild(response.getState(), response.getMessage());
            }
            if (response.getState() == ResponseCodeEnum.NO_PARAM.code()) {
                result.faild(response.getState(), response.getMessage());
            }
        }catch (Exception ex) {
            log.error("login-error:",ex);
            throw ex;
        }
        return result;
    }
}
